﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.OleDb;

namespace Paper_Management_System
{
    public class IContainer
    {
        //定义一个容器接口
        public struct Itm
        {
            //定义结构体，存放一行记录
            public string Title, Author, KeyWords, Abstract;
        }
        public virtual bool Add(Itm tmp)
        {
            //添加一行记录，按末尾存入
            bool rt = true;
            return rt;
        }
        public virtual Itm Get(int idx)
        {
            //获取特定行的记录，存入临时变量
            Itm tmp;
            tmp.Title = "";
            tmp.Author = "";
            tmp.KeyWords = "";
            tmp.Abstract = "";
            return tmp;
        }
        public virtual bool Set(int idx, Itm tmp)
        {
            //用临时变量的值重置特定行的记录
            bool rt = true;
            return rt;
        }
        public virtual int GetCount()
        {
            //获取所有记录的数目，存入变量
            int rt = 0;
            return rt;
        }
        public virtual bool Remove(int idx)
        {
            //删除特定行的记录
            bool rt = true;
            return rt;
        }
        public virtual bool Insert(int idx, Itm tmp)
        {
            //向特定位置插入临时变量保存的记录
            bool rt = true;
            return rt;
        }
        public virtual ArrayList Query(string queryS)
        {
            //以queryS为条件，在所有记录中查询匹配字符串，结果存入临时变量
            ArrayList tmp = new ArrayList();
            return tmp;
        }
        public bool CompString(string targetS, string queryS)
        {
            //字符串匹配函数
            bool rt = false;
            int pos = 0;
            int pLim = targetS.Length - queryS.Length;
            bool bMatch = false;
            while (pos <= pLim)
            {
                if (targetS[pos] == queryS[0])
                {
                    bMatch = true;
                    for (int i = 1; i < queryS.Length; i++)
                    {
                        if (targetS[pos + i] != queryS[i])
                        {
                            bMatch = false;
                            break;
                        }
                    }
                    if (bMatch)
                    {
                        rt = true;
                        break;
                    }
                }
                pos++;
            }
            return rt;
        }
    }

    public class MyALContainer : IContainer
    {
        //容器以ArrayList方式实现
        private ArrayList _data;
        public MyALContainer()
        {
            _data = new ArrayList();
        }
        public override bool Add(Itm tmp)
        {
            bool rt = true;
            _data.Add(tmp);
            return rt;//??如何判断添加成功
        }
        public override Itm Get(int idx)
        {
            Itm tmp;
            tmp = (Itm)_data[idx];
            return tmp;
        }
        public override bool Set(int idx, Itm tmp)
        {
            bool rt = true;
            _data[idx] = tmp;
            return rt;//??
        }
        public override int GetCount()
        {            
            return _data.Count;
        }
        public override bool Remove(int idx)
        {
            bool rt = true;
            _data.RemoveAt(idx);
            return rt;//??
        }
        public override bool Insert(int idx, Itm tmp)
        {
            return base.Insert(idx, tmp);
        }
        public override ArrayList Query(string queryS)
        {
            ArrayList rt = new ArrayList();
            Itm tmp;
            for (int i = 0; i < _data.Count; i++)
            {
                tmp=(Itm)_data[i];
                if (CompString(tmp.Title, queryS) || CompString(tmp.Author, queryS) || CompString(tmp.KeyWords, queryS) || CompString(tmp.Abstract, queryS))
                {
                    rt.Add(tmp);
                }
            }
            return rt;
        }
    }

    public class MyDBContainer : IContainer
    {
        //容器以Access数据库的方式实现
        private OleDbConnection _cnn; 
        public MyDBContainer(string dbFileName)
        {
            //定义数据库连接
            string cnnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbFileName + ";User Id=admin;Password=;";
            _cnn = new OleDbConnection(cnnstr);
        }
        public override bool Add(Itm tmp)
        {
            bool rt = true;
            OleDbCommand cmd = _cnn.CreateCommand();
            cmd.CommandText = "INSERT INTO tb_paperInfo(Title,Author,KeyWords,Abstract) VALUES('"+tmp.Title+"','"+tmp.Author+"','"+tmp.KeyWords+"','"+tmp.Abstract+"')";
            _cnn.Open();
            cmd.ExecuteNonQuery();            
            _cnn.Close();
            return rt;
        }
        public override Itm Get(int idx)
        {
            Itm tmp;
            tmp.Title = "";
            tmp.Author = "";
            tmp.KeyWords = "";
            tmp.Abstract = "";
            int cnt=0;
            OleDbCommand cmd = _cnn.CreateCommand();
            cmd.CommandText = "SELECT * FROM tb_paperInfo";
            _cnn.Open();
            OleDbDataReader reader = cmd.ExecuteReader();            
            while (reader.Read())
            {
                if (cnt == idx)
                {
                    tmp.Title = reader.GetString(1);
                    tmp.Author = reader.GetString(2);
                    tmp.KeyWords = reader.GetString(3);
                    tmp.Abstract = reader.GetString(4);
                    break;
                }
                cnt++;
            }
            _cnn.Close();
            return tmp;
        }
        public override bool Set(int idx, Itm tmp)
        {
            bool rt = true;
            Itm content = this.Get(idx);
            OleDbCommand cmd = _cnn.CreateCommand();
            cmd.CommandText = "UPDATE tb_paperInfo SET [Title]='"+tmp.Title+"',[Author]='"+tmp.Author+"',[KeyWords]='"+tmp.KeyWords+"',[Abstract]='"+tmp.Abstract+"' WHERE [Title]='"+content.Title+"' AND [Author]='"+content.Author+"'";
            _cnn.Open();
            cmd.ExecuteNonQuery();
            _cnn.Close();
            return rt;
        }
        public override int GetCount()
        {
            int rt = 0;
            OleDbCommand cmd = _cnn.CreateCommand();
            cmd.CommandText = "SELECT * FROM tb_paperInfo";
            _cnn.Open();
            OleDbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                rt++;
            }
            _cnn.Close();
            return rt;
        }
        public override bool Remove(int idx)
        {
            bool rt = true;
            Itm content = this.Get(idx);
            OleDbCommand cmd = _cnn.CreateCommand();
            cmd.CommandText = "DELETE FROM tb_paperInfo WHERE ([Title]='"+content.Title+"' AND '"+content.Author+"')";
            _cnn.Open();
            cmd.ExecuteNonQuery();
            _cnn.Close();
            return rt;
        }
        public override bool Insert(int idx, Itm tmp)
        {
            return base.Insert(idx, tmp);
        }
        public override ArrayList Query(string queryS)
        {
            ArrayList tmpAL = new ArrayList();
            Itm tmp;
            tmp.Title = "";
            tmp.Author = "";
            tmp.KeyWords = "";
            tmp.Abstract = "";
            OleDbCommand cmd = _cnn.CreateCommand();
            cmd.CommandText = "SELECT * FROM tb_paperInfo";
            _cnn.Open();
            OleDbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                tmp.Title = reader.GetString(1);
                tmp.Author = reader.GetString(2);
                tmp.KeyWords = reader.GetString(3);
                tmp.Abstract = reader.GetString(4);
                if(CompString(tmp.Title,queryS)||CompString(tmp.Author,queryS)||CompString(tmp.KeyWords,queryS)||CompString(tmp.Abstract,queryS))
                {
                    tmpAL.Add(tmp);
                }
            }
            _cnn.Close();
            return tmpAL;
        }
    }
}
